package a.a013;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

public class Test1 {

	//map、list、javabean、string
	//如果 content 为 "[0]"，则 要求 当前 obj 为 数组 或 list
	public static Object aaa(List<String> contentList, Object obj) throws Exception {
		
		String sss = contentList.remove(0);
		
		
		obj = getValue(sss, obj);
		
		System.out.println(sss);
		
		if(contentList.size() == 0) {
			return obj;
		}else {
			return aaa(contentList, obj);
		}
	}
	
	public static boolean isPrimitive(Object obj) {
		
		if (obj == null) return false;
		
		if(
				obj instanceof Byte			||
				obj instanceof Character	||
				obj instanceof Short 		||
				obj instanceof Integer 		||
				obj instanceof Long 		|| 
				obj instanceof Float 		|| 
				obj instanceof Double 		|| 
				obj instanceof Boolean 
		   ) return true;
		
		else return false;
		
	}

	public static boolean isArray(Object obj) {

		if (obj == null) return false;
		
		return obj.getClass().isArray();

	}
	
	@SuppressWarnings("unchecked")
	public static Object getValue(String path, Object obj) throws Exception {
		
		if(obj == null) {
			return null;
			
		}else if(obj instanceof String || isPrimitive(obj)) {/* String 或 基本类型：则 直接返回 变量自身，即 忽略 path 的值 */
			
			return obj;
			
		}else if(isArray(obj)) {/* 数组，要求 path 为：正整数 */
			
			Object[] objs = (Object[]) obj;
			path = path.replace("[", "").replace("]", "");
			int index = Integer.parseInt(path);
			
			return objs[index];
			
		}else if(obj instanceof List) {/* List，要求 path 为：正整数 */
			
			List<Object> objList = (List<Object>) obj;
			path = path.replace("[", "").replace("]", "");
			int index = Integer.parseInt(path);
			
			return objList.get(index);
			
		}else if(obj instanceof Map) {/* Map，如果找不到 对应的 key，则返回 null */
			
			Map<?, ?> map =   (Map<?, ?>) obj;
			
			return map.get(path);
			
		}else {/* javabean，如果找不到 对应的 property，则返回 null */
			return PropertyUtils.getProperty(obj, path);
		}
	}

	public static void main(String[] args) throws Exception {
		
//		String contentStr = "aaa.bbb.ccc.ddd";
//		String[] contents = contentStr.split(".");
		
//		Foo foo = new Foo(new A(new B(new C(new D()))));
//		
//		ArrayList<String> contentList = new ArrayList<String>(Arrays.asList("aaa","bbb","ccc","ddd"));
//		
//		System.out.println("==> " + aaa(contentList, foo));
//		
		/*
		Object[] objs = new Object[]{};
		String[] strs = new String[]{"zxc", "zxc123", "zxc456"};
		int[] ints = new int[]{};
		
		Object zxc = strs;
		
		if(zxc.getClass().isArray()) {
			Object[] ttt = (Object[]) zxc;
			System.out.println(ttt[2]);
		}
		*/
		
		
//		System.out.println(objs.getClass().isArray());
//		System.out.println(strs.getClass().isArray());
//		System.out.println(ints.getClass().isArray());
//		System.out.println(new Object().getClass().isArray());
		
		
		/**
		Integer a = 123;
		int aa = 456;
		Object aaa = 456;
		List<Integer> aList = new ArrayList<>();
		aList.add(123);
		System.out.println(aList.get(0).getClass().isPrimitive());
		*/
		
		
//		Foo foo = new Foo(new A(new B(new C(new D()))));
//		ArrayList<String> contentList = new ArrayList<String>(Arrays.asList("aaa","bbb","ccc","ddd"));
//		System.out.println("==> " + aaa(contentList, foo));
//		
//		ArrayList<String> foo2 = new ArrayList<String>(Arrays.asList("aaa","zcasd","sad","a4","vsf"));
//		ArrayList<String> contentList2 = new ArrayList<String>(Arrays.asList("[2]"));
//		System.out.println("==> " + aaa(contentList2, foo2));
//		
//		ArrayList<Integer> foo3 = new ArrayList<Integer>(Arrays.asList(12, 23, 45, 634));
//		ArrayList<String> contentList3 = new ArrayList<String>(Arrays.asList("[2]"));
//		System.out.println("==> " + aaa(contentList3, foo3));
//		
//		
////		int foo4 = 1234;
////		ArrayList<String> contentList4 = new ArrayList<String>(Arrays.asList("zxcad"));
////		System.out.println("==> " + aaa(contentList4, foo4));
//		
		
		/**
		A a = new A(new B(), new ArrayList<B>(Arrays.asList(new B("张三"), new B("李四"), new B("王五"), new B("赵六"))));
		ArrayList<String> contentList5 = new ArrayList<String>(Arrays.asList("bbbList", "[2]", "name"));
		System.out.println("==> " + aaa(contentList5, a));
		ArrayList<String> contentList6 = new ArrayList<String>(Arrays.asList("bbbList", "[2]", "name", "poipoucas"));
		System.out.println("==> " + aaa(contentList6, a));
		*/
		
		System.out.println("==> " + isArray(new int[]{}));
		System.out.println("==> " + isArray(new String[]{}));
		System.out.println("==> " + isArray(new Date[]{}));
		System.out.println("==> " + isArray(new Object[]{}));
		
		System.out.println("==> " + isArray(new Object()));
		System.out.println("==> " + isArray(new HashMap<String, Object>()));
		
		
	}
	

}
